//package com.dx.mes.service.spc.webservice;
//
//import com.dx.mes.common.core.rabbitmq.BaseConnFactory;
//import com.dx.mes.common.transcoder.util.JacksonUtil;
//import com.dx.mes.common.transcoder.util.MqOpe;
//import com.dx.mes.service.spc.tx.ex.BaseObj;
//import com.dx.mes.service.spc.tx.ex.Xfrreq;
//import com.rabbitmq.client.AMQP.BasicProperties;
//import com.rabbitmq.client.Channel;
//import com.rabbitmq.client.Connection;
//import com.rabbitmq.client.QueueingConsumer;
//import com.rabbitmq.client.QueueingConsumer.Delivery;
//import org.springframework.stereotype.Controller;
//
//import javax.annotation.Resource;
//import java.io.IOException;
//import java.util.Timer;
//import java.util.TimerTask;
//
//@Controller("providerController")
//public class ProviderController {
//	@Resource
//	private BaseConnFactory baseConnFactory;
//
//	@SuppressWarnings({ "finally", "unchecked" })
//	public <T extends BaseObj> Object MCSSendAndReceive(T t, Object obj) throws Exception{
//		T outTrx = null;
//
//		//创建连接
//		Connection connection = baseConnFactory.creatConnectionFactory().newConnection();
//        // 创建一个频道
//		Channel channel = connection.createChannel();
//		try {
//			//声明队列
//	        channel.queueDeclare("MCS01", true, false, false, null);
//
//	        //发送消息到requestQueueName队列
//	        String message = MqOpe.obj2Str(t);
//			System.out.println("send to MCS : " + message);
//			channel.basicPublish("", "MCS01", new BasicProperties(), message.getBytes("UTF-8"));
//
//	        //为每一个客户端创建一个消费者（用于监听回调队列，获取结果）
//	        QueueingConsumer consumer = new QueueingConsumer(channel);
//	        //消费者与队列关联
//	        StringBuilder replyQueueNameBuilder = new StringBuilder(t.getTrx_id());
//	        replyQueueNameBuilder.setCharAt(2, 'X');
//	        channel.basicConsume(replyQueueNameBuilder.toString(), false, consumer);
//
//        	TimerTask task = new TimerTask() {
//                @Override
//                public void run() {
//        			if(channel.isOpen()){
//        				try {
//							channel.close();
//						} catch (IOException e) {
//							e.printStackTrace();
//						}
//        			}
//
//        			if(connection.isOpen()){
//        				try {
//							connection.close();
//						} catch (IOException e) {
//							e.printStackTrace();
//						}
//        			}
//                    cancel();
//                    System.out.println("Hello !!!");
//                }
//            };
//
//            Timer timer = new Timer();
//            timer.scheduleAtFixedRate(task, 30*1000, 1);
//	        while (true) {
//		        Delivery delivery = consumer.nextDelivery();
//
//				String outMesg = new String(delivery.getBody(),"UTF-8");
//				System.out.println("receive from MCS : " + outMesg);
//				outTrx =  (T) MqOpe.inStr2Obj(outMesg, obj);
//
//		        if (t.getCorrel_id().equals(outTrx.getCorrel_id())) {
//		            channel.basicAck(delivery.getEnvelope().getDeliveryTag(), false);
//		            task.cancel();
//			        break;
//		        }else{
//		        	outTrx = null;
//		        }
//	        }
//
//		} catch(Exception e){
//			throw e;
//		} finally {
//			if(channel.isOpen()){
//				channel.close();
//			}
//
//			if(connection.isOpen()){
//				connection.close();
//			}
//            return outTrx;
//        }
//    }
//
//	public void addXFRREQProc(Xfrreq xfrreq) throws Exception{
//		//创建连接
//		Connection connection = baseConnFactory.creatConnectionFactory().newConnection();
//        // 创建一个频道
//		Channel channel = connection.createChannel();
//		try {
//			//声明队列
//	        channel.queueDeclare("XFRREQ", true, false, false, null);
//	      //发送消息到requestQueueName队列
//	        String message = JacksonUtil.toJSONStr(xfrreq);
//			channel.basicPublish("", "XFRREQ", new BasicProperties(), message.getBytes("UTF-8"));
////	        channel.basicPublish("", "XFRREQ", MessageProperties.PERSISTENT_TEXT_PLAIN, message.getBytes("UTF-8"));
//		} catch (Exception e) {
//			throw e;
//		} finally {
//			if(channel.isOpen()){
//				channel.close();
//			}
//
//			if(connection.isOpen()){
//				connection.close();
//			}
//		}
//	}
//}
